package com.haixiaoke.saas.enterprise.house.controller;

import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.annotation.Log;
import com.haixiaoke.saas.common.constant.PlatformConstants;
import com.haixiaoke.saas.common.core.domain.AjaxResult;
import com.haixiaoke.saas.common.core.page.TableDataInfo;
import com.haixiaoke.saas.common.enums.BusinessType;
import com.haixiaoke.saas.common.utils.poi.ExcelUtil;
import com.haixiaoke.saas.house.domain.House;
import com.haixiaoke.saas.house.service.IHouseService;
import com.haixiaoke.saas.system.BaseController;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 房屋Controller
 * 
 * @author panda
 * @date 2025-06-26
 */
@RestController
@RequestMapping("/enterprise/house/house")
public class EnterpriseHouseController extends BaseController {
    @Autowired
    private IHouseService houseService;

    @GetMapping("/homePage")
    public AjaxResult homePage() {
        return success(houseService.homePage(getLoginUser().getTenantId()));
    }

    @GetMapping("/homePage/deviceLock")
    public AjaxResult homePageDeviceLock() {
        return success(houseService.homePageDeviceLock(getLoginUser().getTenantId()));
    }

    /**
     * 查询房屋列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:house:page')")
    @GetMapping("/page")
    public TableDataInfo list(House house) {
        startPage();
        house.setTenantType(getLoginUser().getTenantId());
        if (StrUtil.equals(PlatformConstants.PC_BUSINESS, getLoginUser().getPlatform())) {
            house.setTenantIds(getTenantIds());
        }
        if (StrUtil.equals(PlatformConstants.MP_WECHAT_BUSINESS, getLoginUser().getPlatform())) {
            house.setTenantId(getLoginUser().getTenantId());
        }
        List<House> list = houseService.selectHouseList(house);
        return getDataTable(list);
    }

    /**
     * 获取房屋详细信息
     */
    @PreAuthorize("@ss.hasPermi('enterprise:house:query')")
    @GetMapping(value = "/{houseId}")
    public AjaxResult getInfo(@PathVariable("houseId") String houseId) {
        return success(houseService.selectHouseByHouseId(houseId));
    }

    /**
     * 新增房屋
     */
    @PreAuthorize("@ss.hasPermi('enterprise:house:add')")
    @Log(title = "房屋", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody House house) {
        house.setTenantId(getLoginUser().getTenantId());
        return houseService.insertHouse(house);
    }

    /**
     * 修改房屋
     */
    @PreAuthorize("@ss.hasPermi('enterprise:house:edit')")
    @Log(title = "房屋", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody House house) {
        house.setTenantId(getLoginUser().getTenantId());
        return houseService.updateHouse(house);
    }

    /**
     * 修改房屋状态
     */
    @PreAuthorize("@ss.hasPermi('enterprise:house:edit')")
    @Log(title = "房屋", businessType = BusinessType.UPDATE)
    @PutMapping("/editState/{houseId}")
    public AjaxResult editState(@PathVariable String houseId) {
        return toAjax(houseService.editState(houseId, getLoginUser().getTenantId()));
    }

    /**
     * 下载楼栋模版
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<House> util = new ExcelUtil<>(House.class);
        util.importTemplateExcel(response, "房屋模版");
    }

    /**
     * 导出企业端角色信息列表
     */
    @PreAuthorize("@ss.hasPermi('enterprise:house:add')")
    @Log(title = "房屋", businessType = BusinessType.IMPORT)
    @PostMapping("/import")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<House> util = new ExcelUtil<>(House.class);
        List<House> houseList = util.importExcel(file.getInputStream());
        String username = getUsername();
        String tenantId = getLoginUser().getTenantId();
        return houseService.importHouse(houseList, username, tenantId);
    }

    /**
     * 删除房屋
     */
    @PreAuthorize("@ss.hasPermi('enterprise:house:remove')")
    @Log(title = "房屋", businessType = BusinessType.DELETE)
	@DeleteMapping("/{houseId}")
    public AjaxResult remove(@PathVariable String houseId) {
        return toAjax(houseService.softDeleteHouseByHouseId(houseId, getLoginUser().getTenantId()));
    }
}
